/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package taskgraph.tasks.data;

import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;

import taskgraph.ports.InputPort;
import taskgraph.ports.OutputPort;
import taskgraph.tasks.PrimitiveTask;


/**
 * Counts the objects received.
 * 
 * The output channel will transport a Long wrapper as Object. Thus the
 * channel is a Channel<Object> and not a Channel<Long>.
 * 
 * <p>This scheme is intended to be compatible with tasks Split and Compose,
 * and other generic tasks. For better performance, custom tasks may be 
 * written using channels that transport native values.
 *
 * @author Armando Blancas
 * @see taskgraph.ports.InputPort
 * @see taskgraph.ports.OutputPort
 * @param <E> The type of elements counted by this task.
 */
public class Count<E> extends PrimitiveTask {

	private InputPort<E> input;
	private OutputPort<Object> output;
	
	/**
	 * This constructor allows creating instances as beans.
	 */
    public Count() {
    	// do nothing
    }

	/**
     * Count constructor.
     * 
     * @param input The element input port.
     * @param output The object output port.
     */
    public Count(final InputPort<E> input, final OutputPort<Object> output) {
    	setInput(input);
    	setOutput(output);
    }

    /**
     * Gets the input port.
     * 
	 * @return The input port.
	 */
	public InputPort<E> getInput() {
		return input;
	}
	
    /**
     * Gets the output port.
     * 
	 * @return The output port.
	 */
	public OutputPort<Object> getOutput() {
		return output;
	}

	/**
     * Performs the work of this task.
	 *<pre>
	 *while not EOF
	 *    read an object reference from input port
	 *    increment count by one
	 *close input port
	 *write count to output port
	 *close output port
	 *</pre>
	 */
	@Override
    public void run() {
    	try {
    		long count = 0;
    		while (!Thread.currentThread().isInterrupted()) {
    			try {
    				input.read();
    			}
    			catch (EOFException e) {
    				break;
    			}
    			++count;
            }
    		output.write(Long.valueOf(count));
        } catch (InterruptedIOException ie) {
        	// interruption request
        } catch (IOException e) {
        	// pipe synch error
            log.error("Count#run()", e);
		} finally {
			input.close();
			output.close();
        }
    }

	/**
	 * Sets the input port.
	 * 
	 * @param input The input port.
	 */
    public void setInput(final InputPort<E> input) {
    	if (input == null)
    		throw new IllegalArgumentException("input == null");
 		this.input = input;
	}

	/**
	 * Sets the output port.
	 * 
	 * @param output The output port.
	 */
    public void setOutput(final OutputPort<Object> output) {
    	if (output == null)
    		throw new IllegalArgumentException("output == null");
 		this.output = output;
	}

}
